package cn.itsource.org.controller;

import cn.itsource.basic.utils.AjaxResult;
import cn.itsource.basic.utils.PageList;
import cn.itsource.org.domain.Department;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.query.EmployeeQuery;
import cn.itsource.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Api(tags={"员工接口"})
@RestController
@RequestMapping("/emp")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;


    /**
     * 查询所有的方法
     */
    @GetMapping
    private AjaxResult findAll(){
        //1.查询所有
        List<Employee> list = employeeService.findAll();
        //2.封装对象返回
        return AjaxResult.createSuccess(list);
    }


    /**
     * 根据id查询所有
     */
    @GetMapping("/findOne/{id}")
    private AjaxResult findOne(@PathVariable("id") Long id){
        return AjaxResult.createSuccess(employeeService.findOne(id));
    }


    /**
     * 分页查询
     */
    @PostMapping ("/findPage")
    public PageList<Employee> getAll(@RequestBody EmployeeQuery query){
        return employeeService.findPage(query);

    }


    /**
     * 查询部门经理
     * @return
     */
    @GetMapping("/findManagers")
    public AjaxResult findManager(){
        List<Employee> list = employeeService.findManagers();
        return AjaxResult.createSuccess(list);
    }

    /**
     * 新增/修改操作
     */
    @PostMapping
    @ApiOperation(value = "新增/修改部门",notes = "王权富贵")
    public AjaxResult saveOrupdate(@ApiParam(value = "新增/修改部门", required = true) Employee employee){
        try {
            employeeService.saveOrUpdate(employee);
            return AjaxResult.createSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.createError("操作失败,请联系小马！！！");
        }
    }

    /**
     * 删除操作
     */
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            employeeService.delete(id);
            return AjaxResult.createSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.createError("操作失败,请联系小马！！！");

        }
    }



}
